package org.example.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.Cleanup;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-11-11 9:44
 */
public class GetConn {
    private static DataSource dataSource;

    static {
        ClassLoader classLoader = GetConn.class.getClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static Connection getConn() throws SQLException {
        return dataSource.getConnection();
    }

    public static void main(String[] args) throws SQLException {
//        Connection conn = GetConn.getConn();
//        System.out.println(conn);
        @Cleanup
        Connection connection = dataSource.getConnection();
        @Cleanup
        PreparedStatement ps = connection.prepareStatement("INSERT INTO employee(username, password) VALUES(?, ?)");
        ps.setString(1, "zs");
        ps.setString(2, "123456");
        ps.executeUpdate();

    }
}
